What is synckit?
The synckit npm package allows for executing asynchronous tasks synchronously using worker threads or child processes. It is designed to offload heavy computation or I/O-bound tasks without blocking the main thread, thus enabling a synchronous-like coding style while maintaining non-blocking behavior.
What are synckit's main functionalities?
Running asynchronous tasks synchronously
This feature allows you to run an asynchronous task inside a worker thread and wait for the result synchronously. The 'worker.js' file should export an async function that will be executed with the provided arguments.
const { runAsWorkerThread } = require('synckit');
const result = runAsWorkerThread('./worker.js', ...args);
Creating a synchronous API from asynchronous functions
With this feature, you can create a synchronous version of an asynchronous function. The 'async-fn.js' file should export an async function that will be executed with the provided arguments, and the result will be returned synchronously.
const { createSyncFn } = require('synckit');
const syncFn = createSyncFn('./async-fn.js');
const result = syncFn(...args);
Other packages similar to synckit
deasync
Deasync turns asynchronous functions into synchronous by blocking the event loop. It is similar to synckit in providing a way to write synchronous-style code, but it does so by pausing the event loop, which can lead to performance issues and is not recommended for production use.
threads
The 'threads' package is used to manage and work with Web Workers and worker threads in Node.js. It offers similar functionality to synckit by allowing asynchronous tasks to be offloaded to separate threads, but it provides a more comprehensive API for managing those threads.
workerpool
Workerpool is a package for managing a pool of workers and running tasks in parallel. It is similar to synckit in that it uses worker threads to execute tasks asynchronously, but it focuses on managing a pool of workers and distributing tasks among them for parallel processing.
synckit
Perform async work synchronously in Node.js using worker_threads
with first-class TypeScript support.
TOC
Usage
Install
yarn add synckit
npm i synckit
API
import { createSyncFn } from 'synckit'
const syncFn = createSyncFn(require.resolve('./worker'), {
tsRunner: 'tsx',
})
const result = syncFn(...args)
import { runAsWorker } from 'synckit'
runAsWorker(async (...args) => {
return result
})
You must make sure, the result
is serializable by Structured Clone Algorithm
Types
export interface GlobalShim {
moduleName: string
globalName?: string
named?: string | null
conditional?: boolean
}
Options
bufferSize
same as env SYNCKIT_BUFFER_SIZE
timeout
same as env SYNCKIT_TIMEOUT
execArgv
same as env SYNCKIT_EXEC_ARGV
tsRunner
same as env SYNCKIT_TS_RUNNER
transferList
: Please refer Node.js worker_threads
documentationglobalShims
: Similar like env SYNCKIT_GLOBAL_SHIMS
but much more flexible which can be a GlobalShim
Array
, see GlobalShim
's definition for more details
Envs
SYNCKIT_BUFFER_SIZE
: bufferSize
to create SharedArrayBuffer
for worker_threads
(default as 1024
)SYNCKIT_TIMEOUT
: timeout
for performing the async job (no default)SYNCKIT_EXEC_ARGV
: List of node CLI options passed to the worker, split with comma ,
. (default as []
), see also node
docsSYNCKIT_TS_RUNNER
: Which TypeScript runner to be used, it could be very useful for development, could be 'ts-node' | 'esbuild-register' | 'esbuild-runner' | 'swc' | 'tsx'
, 'ts-node'
is used by default, make sure you have installed them alreadySYNCKIT_GLOBAL_SHIMS
: Whether to enable the default DEFAULT_GLOBAL_SHIMS_PRESET
as globalShims
TypeScript
ts-node
If you want to use ts-node
for worker file (a .ts
file), it is supported out of box!
If you want to use a custom tsconfig as project instead of default tsconfig.json
, use TS_NODE_PROJECT
env. Please view ts-node for more details.
If you want to integrate with tsconfig-paths, please view ts-node for more details.
esbuild-register
Please view esbuild-register
for its document
esbuild-runner
Please view esbuild-runner
for its document
swc
Please view @swc-node/register
for its document
tsx
Please view tsx
for its document
Benchmark
It is about 20x faster than sync-threads
but 3x slower than native for reading the file content itself 1000 times during runtime, and 18x faster than sync-threads
but 4x slower than native for total time.
And it's almost same as deasync
but requires no native bindings or node-gyp
.
See benchmark.cjs and benchmark.esm for more details.
You can try it with running yarn benchmark
by yourself. Here is the benchmark source code.
Backers
Changelog
Detailed changes for each release are documented in CHANGELOG.md.
License
MIT © JounQin@1stG.me